<template>
  <el-card class="kpi-actual-list">
    <tag-content-header search refresh setting @toolFun="toolFun" :settingData="columns">
      <template #title>
        <span>实时指标API列表</span>
      </template>
    </tag-content-header>
    <tag-table
      ref="table"
      @refresh="getList"
      :data="dataList"
      :loading="loading"
      :columns="columns"
      :params="params"
      :total="totalSize"
      @rowClick="rowClick"
    >
      <template #operation="{ scope }">
        <div class="operation-button-box">
          <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
            <icon-svg
              name="iconEdit"
              class="svg-edit"
              @click.stop.native="handleEdit('edit', scope.row)"
              v-if="$auth('index:indexApi:update')"
            ></icon-svg>
          </el-tooltip>
          <el-tooltip effect="dark" content="测试" placement="top" :enterable="false">
            <icon-svg
              name="iconDevelop"
              class="svg-view"
              @click.stop.native="handleEdit('view', Object.assign({}, scope.row, { tab: 'test' }))"
            ></icon-svg>
          </el-tooltip>
        </div>
      </template>
    </tag-table>
    <actual-api-drawer ref="actualDrawer" @refresh="getList"></actual-api-drawer>
  </el-card>
</template>

<script>
import actualApiDrawer from './actual-api-drawer'
export default {
  components: {
    actualApiDrawer
  },
  data() {
    return {
      dataList: [],
      loading: false,
      columns: [
        {
          show: true,
          label: 'API名称',
          prop: 'apiNm',
          search: true,
          searchKey: ''
        },
        {
          show: true,
          label: '请求类型',
          prop: 'apiReqType',
          dict: '$api_request_type',
          align: 'center'
        },
        {
          show: true,
          label: 'API说明',
          prop: 'apiDesc',
          align: 'center'
        },
        {
          show: true,
          label: '创建时间',
          prop: 'createTm',
          align: 'center'
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 200
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    rowClick(row) {
      if (row.apiId) this.handleEdit('view', row)
    },
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('index/indexApi/page'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          if (data.data.currentPageNum === this.params.currentPageNum) {
            this.dataList = data.data.records
            this.totalSize = data.data.totalSize
          }
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    handleEdit(action = 'view', scope = {}) {
      let params = { action: action }
      if (scope.apiId) params.apiId = scope.apiId
      if (scope.tab) params.tab = scope.tab
      this.$refs.actualDrawer.showDrawer(params)
    }
  }
}
</script>

<style lang="scss">
.kpi-actual-list {
  .tag-table .el-table__row {
    cursor: pointer;
  }
}
</style>
